//
//  GenerateDatePickerViewController.m
//  GenerateDatePicker
//
//  Created by Naoki TSUTSUI on 10/02/06.
//  Copyright 2010 iphoneworld.jp. All rights reserved.
//

#import "GenerateDatePickerViewController.h"


@implementation GenerateDatePickerViewController
@synthesize datePicker;
@synthesize dateTableView;
@synthesize date;
@synthesize delegate;

- (void)dealloc {
  [datePicker release];
  [dateTableView release];
  [date release];
  [super dealloc];
  
}


-(IBAction)dateChanged {
  self.date = [datePicker date];
  [dateTableView reloadData];
}

-(IBAction)cancel {
  // [[self.delegate navController] popViewControllerAnimated:YES];
  [self.navigationController popViewControllerAnimated:YES];
}

-(IBAction)save {
  [self.delegate takeNewDate:date];
  // [[self.delegate navController] popViewControllerAnimated:YES];
  [self.navigationController popViewControllerAnimated:YES];
}



- (void)loadView
{
  UIView *theView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  self.view = theView;
  [theView release];
  
  UITableView *theTableView;
  theTableView = [[UITableView alloc] 
                  initWithFrame:CGRectMake(0.0, 67.0, 320.0, 480.0) 
                          style:UITableViewStyleGrouped];
  theTableView.delegate = self;
  theTableView.dataSource = self;
  [self.view addSubview:theTableView];
  self.dateTableView = theTableView;
  [theTableView release];
  
  UIDatePicker *theDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 200.0, 320.0, 216.0)];
  theDatePicker.datePickerMode = UIDatePickerModeDate;
  self.datePicker = theDatePicker;
  [theDatePicker release];
  [datePicker addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged];
  [self.view addSubview:datePicker];
  
  
  UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]
                                   initWithTitle:@"キャンセル"
                                   style:UIBarButtonItemStylePlain
                                   target:self
                                   action:@selector(cancel)];
  self.navigationItem.leftBarButtonItem = cancelButton;
  [cancelButton release];
  
  UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
                                 initWithTitle:@"保存"
                                 style:UIBarButtonItemStylePlain
                                 target:self
                                 action:@selector(save)];
  self.navigationItem.rightBarButtonItem = saveButton;
  [saveButton release];
  
  self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
}

/*
- (id)initWithStyle:(UITableViewStyle)style {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if (self = [super initWithStyle:style]) {
    }
    return self;
}
*/

/*
- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
*/


- (void)viewWillAppear:(BOOL)animated {
  if (self.date != nil)
    [self.datePicker setDate:date animated:YES];
  else 
    [self.datePicker setDate:[NSDate date] animated:YES];
  
  [super viewWillAppear:animated];
}

/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/


// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
  static NSString *CellIdentifier = @"DateCellIdentifier";
    
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    cell.textLabel.font = [UIFont systemFontOfSize:17.0];

    cell.textLabel.textColor = [UIColor colorWithRed:0.243 green:0.306 blue:0.435 alpha:1.0];
      
  }
    
  // Set up the cell...
  NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//  [formatter setDateFormat:@"MMMM dd, yyyy"];
  [formatter setDateFormat:@"yyyy年 MM月 dd日"];
  cell.textLabel.text = [formatter stringFromDate:date];
  [formatter release];
	
  return cell;
}

/*
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController];
	// [anotherViewController release];
}
*/
@end

